package com.example.studentknowledgesys.controller;


import com.example.studentknowledgesys.entity.User;
import com.example.studentknowledgesys.entity.UserProfile;
import com.example.studentknowledgesys.service.UserProfileService;
import com.example.studentknowledgesys.service.UserService;
import com.example.studentknowledgesys.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserProfileService  userProfileService;

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        System.out.println("pppp:" + user);
        User login = userService.login(user.getUsername(), user.getPassword());
        if (login == null) {
            return Result.error(400, "用户名或密码错误");
        }
        Map<String, Object> data = new HashMap<>();
        data.put("userId", login.getId());
        UserProfile userProfile = userProfileService.getUserProfileByUserId(login.getId());
        data.put("role", login.getRole());
        data.put("username", login.getUsername());
        data.put("avatarUrl", userProfile.getAvatarUrl());
        return Result.success(data);
    }

    // 处理登出请求
    @GetMapping("/logout")
    public String logout() {
        // 这里可以进行注销处理（比如清除会话）
        return "login";  // 注销后返回登录页面
    }
}
